import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './firstRouter'

Vue.use(VueRouter)


// 实例化路由
const router = new VueRouter({
  routes
})

// 判断是否存在账户
let isAuthenticated = Boolean(localStorage.getItem('Author')) || false

// 路由守卫，如果浏览器没有用户token缓存，就跳到登录页
router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' })
  }
  else {
    next()
  }
})

export default router
